// src/main/java/com/example/daibanshiwu/Service/Impl/LoginServiceImpl.java
package com.example.daibanshiwu.Service.Impl;

import com.example.daibanshiwu.Service.LoginService;
import com.example.daibanshiwu.mapper.LoginMapper;
import com.example.daibanshiwu.pojo.TUser;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    private final LoginMapper loginMapper;

    public LoginServiceImpl(LoginMapper loginMapper) {
        this.loginMapper = loginMapper;
    }

    @Override
    public TUser login(TUser loginUser) {
        // 1. 根据从传入对象中获取的账号查询用户
        TUser userFromDb = loginMapper.findByAccount(loginUser.getAccount());

        // 2. 检查用户是否存在
        if (userFromDb == null) {
            throw new RuntimeException("账号或密码错误");
        }

        // 3. 验证密码是否匹配
        // 【安全警告】: 生产环境绝不能使用明文比较！
        if (!userFromDb.getPassword().equals(loginUser.getPassword())) {
            throw new RuntimeException("账号或密码错误");
        }

        // 4. 验证通过，返回从数据库查到的完整用户信息
        return userFromDb;
    }
}
